package com.lk.springboot.controller;

import com.lk.springboot.bean.ValidatorBeen;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lk
 * @date 2020/2/1 0001 14:50
 */
@RestController
public class ValidatorController {

    // 参数验证，检测
    @RequestMapping(value = "/testvalid",method = RequestMethod.POST)
    public String examine(@Valid ValidatorBeen validatorBeen, BindingResult bindingResult){
        StringBuffer errorStringBuffer = new StringBuffer();
        if (bindingResult.hasErrors()){
            List<ObjectError> list = bindingResult.getAllErrors();
            for (ObjectError objectError : list) {
                errorStringBuffer.append(objectError.getDefaultMessage() + "--------");
            }
        }
        return errorStringBuffer.toString().equals("") ? "没有错误":errorStringBuffer.toString();
    }
}
